package Gui;

import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import org.jfree.ui.RefineryUtilities;

import client.LibaryMain;

import javax.swing.JComboBox;


/**
 * this class implements gui lending report window
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class LendReport extends JPanel{
	
	
	
	private JTable table;
	public LendReport(String [] str) {
		setBackground(new Color(153, 204, 255));
		setSize(1000,702);
		init(str);
		
	}
	/**
	 * initilize the lend report window 
	 * @param str
	 */
	public void init(String [] str){
		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		setLayout(null);
		for (int i=1; i<=(str.length-6);i=i+6){
	        Vector<Object> row = creatrow(str,i);
	         data.add(row);
	        }
		
		
		
		
		final JComboBox<Object>  comboBox = new JComboBox();
		for ( int w = 1 ; w <= str.length-6; w=+w+6)
			comboBox.addItem(str[w+5]);
		comboBox.setBounds(493, 385, 134, 22);
		add(comboBox);
		
		final JComboBox<Object>  comboBox1 = new JComboBox();
		for ( int w = 1 ; w <= str.length-6; w=+w+6)
			comboBox1.addItem(str[w+5]);
		comboBox1.setBounds(212, 385, 134, 22);
		add(comboBox1);
		
			System.out.println(">>>>>filling table>>>>>>>>");
	        Vector<String> headers = new Vector<String>();
	      
	     
		     headers.add("Report ID");
		     headers.add("Avarge Wanted");
		     headers.add("Midean Wanted");
		     headers.add("Avarge Not Wanted");
		     headers.add("Midean Not Wanted");
		     headers.add("Date");
	   
	     
	       
		JLabel lblNewLabel = new JLabel("Lend Report");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblNewLabel.setBounds(23, 11, 172, 25);
		add(lblNewLabel);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(33, 106, 694, 217);
		add(scrollPane_1);
		
		  final JTable table_1 = new JTable( data, headers)
		  {
		         public boolean isCellEditable(int data, int columns)
		         {
		             if(columns>=0)
		      	     return false;
		             else return true;
		         }
		  };
		  table_1.addMouseListener(new MouseAdapter() {
		  	@Override
		  	public void mouseClicked(MouseEvent arg0) {
		  		int row= table_1.getSelectedRow();
		  	   
		  	  
		  	    int AvargeWanted=Integer.parseInt((String) table_1.getValueAt(row,1));
		  	    int midWanted= Integer.parseInt((String) table_1.getValueAt(row,2));
		  	    int Avargenormal=Integer.parseInt((String) table_1.getValueAt(row,3));
		  	    int midnormal=Integer.parseInt((String) table_1.getValueAt(row,4));
		  	    		  	    
		  	    
		  	    final LendHistogram histo =new LendHistogram("Lend Diagram",AvargeWanted,midWanted,Avargenormal,midnormal);
		  		RefineryUtilities.centerFrameOnScreen(histo);
		  		histo.setSize(400,200);
		  		histo.setLocation(500, 500);
		  		histo.setVisible(true);
		  		histo.pack();
		  	    RefineryUtilities.centerFrameOnScreen(histo);
		  	    histo.setVisible(true);
		  	    histo.toFront();
		  		
		  	}
		  });
		  scrollPane_1.setViewportView(table_1);
		
		JButton btnNewButton = new JButton("back");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainScreen.cardLayot.show(MainScreen.card, "reportmenu");
			}
		});
		
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnNewButton.setBounds(23, 635, 89, 23);
		add(btnNewButton);
		
		JLabel lblNewLabel_1 = new JLabel("Strat Date");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_1.setBounds(96, 387, 96, 14);
		add(lblNewLabel_1);
		
		JLabel lblEndDate = new JLabel("End Date");
		lblEndDate.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblEndDate.setBounds(406, 389, 77, 14);
		add(lblEndDate);
		
		JButton btnGeneratReport = new JButton("Generat Report");
		btnGeneratReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String start,end;
				start=(String)comboBox1.getSelectedItem();
				end=(String)comboBox.getSelectedItem();
				if(start.compareTo(end)<=0){
				ArrayList<Object> Arr=new ArrayList<Object>();
				Arr.add("generatreportbydate");
				//Object o=comboBox.getSelectedItem();
				Arr.add(comboBox1.getSelectedItem());
				Arr.add(comboBox.getSelectedItem());
				LibaryMain.chat.handleMessageFromGui(Arr);
				}
				else{
					JOptionPane.showMessageDialog(null, "Please Enter Start Date Earlier Then End Date ");
				}
								
			}
		});
		btnGeneratReport.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnGeneratReport.setBounds(681, 371, 149, 47);
		add(btnGeneratReport);
		
		JLabel lblNewLabel_2 = new JLabel("Press On A Row To Generate Report");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_2.setBounds(23, 47, 298, 14);
		add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Or Select Dates Below And Press Generate");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_3.setBounds(23, 72, 373, 14);
		add(lblNewLabel_3);
		
		
	}
	private Vector<Object> creatrow(String[] str, int i) {
		 Vector<Object> row = new Vector<Object>();
		 row.add( ""+str[i]);
		 row.add( ""+str[i+1]);
		 row.add( ""+str[i+2]);
		 row.add( ""+str[i+3]);
		 row.add( ""+str[i+4]);
		 row.add( ""+str[i+5]);
		
		return row;
	}
}
